package com.zrf.gulimall.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.common.database.utils.Query;
import com.zrf.gulimall.product.dao.CategoryBrandRelationDao;
import com.zrf.gulimall.product.entity.BrandEntity;
import com.zrf.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zrf.gulimall.product.entity.CategoryEntity;
import com.zrf.gulimall.product.service.BrandService;
import com.zrf.gulimall.product.service.CategoryBrandRelationService;
import com.zrf.gulimall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

	@Resource
	private BrandService brandService;
	@Resource
	private CategoryService categoryService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryBrandRelationEntity> page = this.page(
				new Query<CategoryBrandRelationEntity>().getPage(params),
				new QueryWrapper<CategoryBrandRelationEntity>()
		);

		return new PageUtils(page);
	}

	@Override
	public List<CategoryBrandRelationEntity> queryByCatalogId(List<Long> catalogId) {
		return this.lambdaQuery()
				.in(CollUtil.isNotEmpty(catalogId), CategoryBrandRelationEntity::getCatelogId, catalogId)
				.list();
	}

	@Override
	public List<CategoryBrandRelationEntity> queryByBrandId(List<Long> brandId) {
		return this.lambdaQuery()
				.in(CollUtil.isNotEmpty(brandId), CategoryBrandRelationEntity::getBrandId, brandId)
				.list();
	}

	@Override
	public void updateCatalog(Long catalogId, String catalogName) {
		this.lambdaUpdate()
				.set(StrUtil.isNotBlank(catalogName), CategoryBrandRelationEntity::getCatelogName, catalogName)
				.eq(CategoryBrandRelationEntity::getCatelogId, catalogId)
				.update();
	}

	@Override
	public void updateBrand(Long brandId, String brandName) {
		this.lambdaUpdate()
				.set(StrUtil.isNotBlank(brandName), CategoryBrandRelationEntity::getBrandName, brandName)
				.eq(CategoryBrandRelationEntity::getBrandId, brandId)
				.update();
	}

	@Override
	public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		BrandEntity brandEntity = brandService.getById(brandId);
		Long catelogId = categoryBrandRelation.getCatelogId();
		CategoryEntity categoryEntity = categoryService.getById(catelogId);
		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		baseMapper.insert(categoryBrandRelation);
	}

}